package com.patrick.open.api.core.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @author patrick
 * @date 2020/6/1 11:41 下午
 * @Des 自动填充
 * 最簡單的事是堅持，最難的事還是堅持
 */
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

  @Override
  public void insertFill(MetaObject metaObject) {
    log.info("start insert fill ....");
    Object createTime = this.getFieldValByName("createTime", metaObject);
    if (null == createTime) {
      this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
    }
    Object updateTime = this.getFieldValByName("updateTime", metaObject);
    if (null == updateTime) {
      this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
    }

  }

  @Override
  public void updateFill(MetaObject metaObject) {
    log.info("start update fill ....");
    Object updateTime = this.getFieldValByName("updateTime", metaObject);
    if (null == updateTime) {
      this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
    }
  }
}
